import { normalizeContainer } from '../../../element/Func';
import { set_dragEl_top_left } from '../Classes/cla_direct_drag_operate/Classes/cla_direct_drag_operate';
import { drag_cssVar_left, drag_cssVar_top } from '../DataDefine/sourceData';

const drag_win_load_set_get_tag = 'drag_win_load_set_get';

let top;

let left;

class drag_win_load_set_get {
  get [Symbol.toStringTag]() {
    return drag_win_load_set_get_tag;
  }

  /**
   *
   * @param {HTMLElement} element dragEl 拖拽元素
   * @param {string | Number} spacename_top win_load 私有 top 属性 命名
   * @param {string | Number} spacename_left win_load 私有 left 属性 命名
   */
  constructor(element, spacename_top, spacename_left) {
    this.element = normalizeContainer(element);

    if (element == null) {
      console.log('dragEl is null');
    }

    this.spacename_top = spacename_top;

    this.spacename_left = spacename_left;
  }

  element;

  spacename_top;

  spacename_left;

  /**
   * 设定 win_load top left 信息
   */
  set_cur_posi() {
    top = this.element.style.getPropertyValue(drag_cssVar_top);
    left = this.element.style.getPropertyValue(drag_cssVar_left);

    sessionStorage.setItem(this.spacename_top, top);
    sessionStorage.setItem(this.spacename_left, left);
  }

  /**
   * 获取 win_load 上的 top left 信息
   * @returns
   */
  get_win_load_posi() {
    top = sessionStorage.getItem(this.spacename_top, top);
    left = sessionStorage.getItem(this.spacename_left, left);

    return { top, left };
  }

  /**
   * 装载 当前 win_load 位置信息
   */
  init_win_load_posi() {
    ({ top, left } = this.get_win_load_posi());

    if (top != null || left != null) {
      top = top || 0;
      left = left || 0;
      set_dragEl_top_left(this.element, top, left);
    }
  }
}

export { drag_win_load_set_get };
